package com.hdl.service.impl;

import com.hdl.dao.AccountDao;
import com.hdl.pojo.Account;
import com.hdl.service.TransferService;

/**
 * @author hedonglin
 * @date 2020/5/3 11:52
 * @description 转账接口实现类
 */
public class TransferServiceImpl implements TransferService {
//    private AccountDao accountDao = new JdbcAccountDaoImpl();
//    private AccountDao accountDao = (AccountDao) BeanFactory.getBean("accountDao");
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
//        try {
//            TransactionManager.getInstance().beginTransaction();
            Account from = accountDao.queryAccountByCardNo(fromCardNo);
            Account to = accountDao.queryAccountByCardNo(toCardNo);

            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);

            accountDao.updateAccountByCardNo(to);
//            int i = 1/0;
            accountDao.updateAccountByCardNo(from);
//            TransactionManager.getInstance().commit();
//        }catch (Exception e){
//            TransactionManager.getInstance().rollback();
//            throw e;
//        }
    }
}
